package com.corx.network.mysqlstorage.repository;


import com.corx.network.mysqlstorage.common.JdbcPagingSupport;
import com.corx.network.mysqlstorage.common.PageQuery;
import com.corx.network.mysqlstorage.common.PageResult;
import com.corx.network.mysqlstorage.common.SqlQueryBuilder;
import com.corx.network.mysqlstorage.model.DeviceDataRaw;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.Optional;

/**
 * @author gx
 * @version 1.0.0
 */
public class JdbcDeviceDataRawRepository extends JdbcPagingSupport<DeviceDataRaw> implements DeviceDataRawRepository {

    public JdbcDeviceDataRawRepository(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    protected RowMapper<DeviceDataRaw> getRowMapper() {
        return (rs, rowNum) -> {
            DeviceDataRaw data = new DeviceDataRaw();
            data.setId(rs.getLong("id"));
            data.setDeviceId(rs.getLong("device_id"));
            data.setRawJson(rs.getString("raw_data"));
            data.setCreateTime(rs.getTimestamp("create_time"));
            data.setUpdateTime(rs.getTimestamp("update_time"));
            return data;
        };
    }

    @Override
    public void save(@NonNull DeviceDataRaw model) {
        String sql = "INSERT INTO corx_device_data_raw (device_id, model_id, raw_json, create_time, update_time) VALUES (?, ?, ?, NOW(), NOW())";
        jdbcTemplate.update(sql, model.getDeviceId(), model.getModelId(), model.getRawJson());
    }

    @Override
    public Optional<DeviceDataRaw> findById(@NonNull Long id) {
        List<DeviceDataRaw> list = jdbcTemplate.query("SELECT * FROM corx_device_data_raw WHERE id = ?", getRowMapper(), id);
        return Optional.ofNullable(DataAccessUtils.singleResult(list));
    }

    @Override
    public List<DeviceDataRaw> findByDeviceId(@NonNull Long deviceId) {
        List<DeviceDataRaw> list = jdbcTemplate.query("SELECT * FROM corx_device_data_raw WHERE device_id = ?", getRowMapper(), deviceId);
        return list;

    }

    @Override
    public void update(@NonNull DeviceDataRaw model) {
        String sql = "UPDATE corx_device_data_raw SET device_id = ?, model_id = ?, raw_json = ?, update_time = NOW()   WHERE id = ?";
        jdbcTemplate.update(sql, model.getDeviceId(), model.getModelId(), model.getRawJson(), model.getId());
    }

    @Override
    public void delete(@NonNull Long id) {
        jdbcTemplate.update("DELETE FROM corx_device_data_raw WHERE id = ?", id);
    }

    @Override
    public PageResult<DeviceDataRaw> findByPage(@NonNull DeviceDataRaw model, @NonNull PageQuery pageQuery) {
        SqlQueryBuilder queryBuilder = new SqlQueryBuilder("SELECT * FROM corx_device_data_raw");


        if (model.getDeviceId() != null) {
            queryBuilder.whereEquals("device_id", model.getDeviceId());
        }
        if (model.getModelId() != null) {
            queryBuilder.whereEquals("model_id", model.getModelId());
        }
        if (pageQuery.hasSort()) {
            queryBuilder.orderBy(pageQuery.orderBy());
        }
        // 构建 SQL 和参数
        String baseSql = queryBuilder.getSql();
        Object[] params = queryBuilder.getParams();

        return super.findByPage(baseSql, params, pageQuery);
    }
}


